#!/bin/sh

# must not fail
set -e 

# debug
#set -x 

echo "Starting $0"

HTMF_DISTNAME=hardtokenmgmt_1.4.0.zip
HTMF_DIRNAME=hardtokenmgmt_1.4.0
HTMF_SOFTLINK=htmf
HTMF_UNPACKCMD=unzip
JARSIGNER=jarsigner.jks

HTMF_CUSTOM_DISTNAME=htmf_custom_project_1.4.0.zip
HTMF_CUSTOM_DIRNAME=htmf_custom_project_1.4.0
HTMF_CUSTOM_SOFTLINK=htmf_custom_project
HTMF_CUSTOM_UNPACKCMD=unzip


### htmf

if [ ! -f ${DISTFILES_INSTALL}/${HTMF_DISTNAME} ] ; then
    echo "copying ${HTMF_DISTNAME} to ${DISTFILES_INSTALL}"
    cp ${HTMF_DISTNAME}      ${DISTFILES_INSTALL}
fi


if [ -x ${HTMF_DIRNAME} ] ;then
    echo "${HTMF_DIRNAME} exists, skipping"
else
   
    cd ${INSTALL_INTO}
    ${HTMF_UNPACKCMD} ${DISTFILES_INSTALL}/${HTMF_DISTNAME}
    cd -
fi


rm -f ${HTMF_SOFTLINK}
ln -s ${HTMF_DIRNAME} ${INSTALL_INTO}/${HTMF_SOFTLINK}

### htmf custom
if [ ! -f ${DISTFILES_INSTALL}/${HTMF_CUSTOM_DISTNAME} ] ; then
    echo "copying ${HTMF_CUSTOM_DISTNAME} to ${DISTFILES_INSTALL}"
    cp ${HTMF_CUSTOM_DISTNAME}   ${DISTFILES_INSTALL}
fi


if [ -x ${HTMF_CUSTOM_DIRNAME} ] ;then
    echo "${HTMF_CUSTOM_DIRNAME} exists, skipping"
else
    cd ${INSTALL_INTO}
    ${HTMF_CUSTOM_UNPACKCMD} ${DISTFILES_INSTALL}/${HTMF_CUSTOM_DISTNAME}
    cd -
fi

rm -f ${HTMF_CUSTOM_SOFTLINK}
ln -s ${HTMF_CUSTOM_DIRNAME} ${INSTALL_INTO}/${HTMF_CUSTOM_SOFTLINK}



### add to environment
echo "export HTMF_HOME=${INSTALL_INTO}/htmf" >> ${INSTALL_INTO}/environment
echo "export HTMF_DIRNAME=${HTMF_DIRNAME}" >> ${INSTALL_INTO}/environment
echo "export HTMF_CUSTOM_DIRNAME=${HTMF_CUSTOM_DIRNAME}" >> ${INSTALL_INTO}/environment


### chmod
chown -R  ${appsrvuser}.${appsrvgrp} ${INSTALL_INTO}/${HTMF_DIRNAME}




echo "End of $0"


